package homework.day02;

import java.io.*;

/**
 * @Classname: Homework03
 * @Author: bromide
 * @CreateTime: 2022/4/1--18:48
 * @Version: V1.0
 * @Description: 将当前目录下的所有obj文件获取到，并进行
 * 反序列化后输出每个用户的信息(直接输出反序
 * 列化后的User对象即可)
 */
public class Homework03 {
    public static void main(String[] args) {
        File dir = new File("./");
        if (dir.isDirectory()) {
            //先将.obj结尾的文件筛选出来
            File[] subs = dir.listFiles(file -> file.getName().endsWith(".obj"));
            for (int i = 0; i < subs.length; i++) {
                try {
                    FileInputStream fis = new FileInputStream(subs[i]);
                    ObjectInputStream bis = new ObjectInputStream(fis);
                    Object object = bis.readObject();
                    if (object instanceof User) {
                        User user = (User) object;
                        System.out.println(user.toString());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
